import cn.yunhe.entity.Admin;
import cn.yunhe.entity.Topic;
import cn.yunhe.services.ITopicServices;
import cn.yunhe.services.impl.TopicServices;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Demo {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    ITopicServices iTopicServices = null;

    @Before
    public void bef() {
        iTopicServices = (ITopicServices) ac.getBean("topicServices");
    }

    @Test
    public void add() {
        Topic topic = new Topic();
        topic.setTitle("spring1");
        topic.setContext("spring整合hibernate1");
        iTopicServices.add(topic);
    }

    @Test
    public void up() {
        Topic topic = new Topic();
        topic.setTid(17);
        topic.setTitle("springUpdate");
        topic.setContext("spring整合hibernate_Update功能");
        iTopicServices.up(topic);
    }

    @Test
    public void searchByArgs() {
        List<Topic> list = iTopicServices.searchByArgs("from Topic");
        for (Topic t : list) {
            System.out.println(t.getTitle());
        }
    }

    @Test
    public void del() {
        iTopicServices.del(19);
    }

    @Test
    public void search() {
        Topic topic = new Topic();
        topic.setTitle("spring");
        List<Topic> list = iTopicServices.search(topic);
        for (Topic t : list
        ) {
            System.out.println(t.getTitle());
        }
    }

    @Test
    public void txAdd() {
        List<Topic> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Topic topic = new Topic();
            //topic.setTitle("TX"+i);
            if (i == 2) {
                topic.setTitle(null);
            } else {
                topic.setTitle("TX" + i);
            }
            list.add(topic);

        }
        iTopicServices.add(list);
    }
}
